﻿-- =============================================
-- Author:		<赵洪涛>
-- Create date: <2012-07-02>
-- Description:	<批量设置成单维护客服>
-- =============================================
CREATE PROCEDURE [dbo].[proc_Project_BatUpdate_TrackKf] 
(
	@Order_Id varchar(4000),
	@TrackKfAccount varchar(20),
	@TrackKfName nvarchar(50),
	@TrackKfDepId int,
	@CompanyId int
)
AS
Begin
	Declare @cSql varchar(5000)
	
	--By ZHT 2015-01-27 更新未完结任务的下发人(选中的客户的原维护客服下发的尚未完结的任务)
	Set @cSql = 'Update a Set DownPerson='''+@TrackKfAccount+''',DownPersonName='''+@TrackKfName+'''
	From Project_Task a,(Select Distinct OId,TrackKfAccount From [Order] Where CusId In('+@Order_Id+') And TrackKfAccount<>'''') b
	Where a.OrderId=b.OId And a.DownPerson=b.TrackKfAccount And a.IsFinish=0'
	Exec(@cSql)

	--更新成单表中同一客户的所有成单
	Set @cSql = 'Update [Order] Set TrackKfAccount='''+@TrackKfAccount+''',TrackKfName='''+@TrackKfName+
	''',TrackKfDepId='+Convert(varchar(10),@TrackKfDepId)+'
	Where CompanyId='+Convert(varchar(10),@CompanyId)+' And CusId In('+@Order_Id+')'
	Exec(@cSql)

	--2013.11.12 顾世杰 分配客服，修改客户表中的客服字段
	Set @cSql = 'Update Customer Set ServicePerson='''+@TrackKfAccount+''',ServicePersonEName='''+@TrackKfName+
	''',ServiceDate=GetDate() Where CompanyId='+
	Convert(varchar(10),@CompanyId)+' And CustomerId In('+@Order_Id+')'
	Exec(@cSql)
End